package com.txby.pf.modules.job.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jck.mybatis.mapper.MybatisBaseDaoCRUD;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.utils.SpringContextUtils;
import com.txby.pf.modules.job.service.impl.DeviceRemindJobService;
import com.txby.pf.modules.sys.service.SysDeptService;
import com.txby.pf.modules.sys.service.SysUserService;
import com.txby.platformCore.component.service.PropertySourceConfig;
import com.txby.platformCore.component.util.GTPushUtil;
import com.txby.platformCore.component.util.XGPushUtil;
import com.txby.platformCore.data.CommonXGRetunInfo;
import com.txby.platformCore.entity.DeviceGTServerSign;
import com.txby.platformCore.entity.DeviceRemindJob;
import com.txby.platformCore.entity.DeviceRemindJobMsg;
import com.txby.platformCore.entity.DeviceXGServerSign;

/**
 * xg推送服务提醒定时任务 
 * 
 * remindTask为spring bean的名称
 *
 */
@Component("remindTask")
public class RemindTask {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private SysUserService sysUserService;
	@Resource(name = "mybatisBaseDaoCRUD")
	public MybatisBaseDaoCRUD mBaseDao;
	@Autowired
	private DeviceRemindJobService scheduleJobService;
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private PropertySourceConfig ps;
	
	public void executeRemind(String params,Long jobId){
		Map<Integer,Object>  customeParam =new HashMap();
		customeParam.put(0, "点餐提醒");
    	customeParam.put(1, "吃药提醒");
    	customeParam.put(2, "康复活动提醒");
		String sql="select * from "+this.mBaseDao.tableName(DeviceRemindJob.class)+" where id_="+jobId+"";
		DeviceRemindJob drjob=(DeviceRemindJob) this.mBaseDao.findBySqlUniqueObject(sql, DeviceRemindJob.class, null);
		DeviceXGServerSign dxgs=SpringContextUtils.getDeviceXGServerSign();
		  DeviceGTServerSign dxs=SpringContextUtils.getDeviceGTServerSign();
		XGPushUtil xgu=XGPushUtil.getNewInstance(new Long(dxgs.getAccessId()), dxgs.getSecretKey());
		GTPushUtil xu=	GTPushUtil.getNewInstance(dxs.getAppID(), dxs.getAppKey(), dxs.getMasterSecret());
			if(!ChkUtil.isEmpty(drjob)){
				sql="select t.bedNO,t.deviceSN,t2.token from   t_txby_productDeviceInit  t, t_txby_deviceXGtoken t2,t_txby_sickDetailInfo t3 WHERE   t.deviceSN=t2.deviceSN and t.bedNO=t3.bedNO and t.deptId=t.deptId and t.sickAreaNO=t3.sickAreaNO  and t3.outHosTime is null and   t.bedNO='"+drjob.getBedNO()+"' and t.deptId='"+drjob.getDeptId()+"' and t.sickAreaNO="+drjob.getSickAreaNO()+" ";
				Map data=(Map) this.mBaseDao.findBySqlUniqueObject(sql, null, null);
				if(!ChkUtil.isEmpty(data.get("token"))){
					DeviceRemindJobMsg msg=new DeviceRemindJobMsg();
					try {
						BeanUtils.copyProperties(msg, drjob);
						msg.setId_(null);
						msg.setTitle(drjob.getParams());
						msg.setDeviceSN(data.get("deviceSN").toString());
						Date date=new Date();
						msg.setCreateTime(date);
						msg.setUpdateTime(date);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					int msgId=this.mBaseDao.saveObject(msg);
					//推送消息
	    			Map m=new HashMap();
	    			Map custom=new HashMap();
	    			custom.put("remindType", drjob.getRemindType());
	    			custom.put("discription", customeParam.get(drjob.getRemindType()));
	    			custom.put("msgId", msgId);
//	    			custom.put("data", m);
//					CommonXGRetunInfo result=xgu.pushSingleDeviceCustomMessage(data.get("token").toString(), drjob.getParams(), drjob.getContent(), 2, drjob.getRemindType());
//					CommonXGRetunInfo  result=xgu.pushSingleDeviceCustomMessage(data.get("token").toString(), drjob.getParams(), drjob.getContent(), custom, 2);
					CommonXGRetunInfo  result=xu.pushSingleDeviceCustomMessage(data.get("token").toString(), custom);
				System.out.println("推送信息为："+drjob.getContent());
				System.out.println("推送状态为："+result);
				}
				
			}else{
				
				String jogin=sysDeptService.getSubDeptIdList(new Long(drjob.getDeptId()));
				sql="select t.bedNO,t.deviceSN,t2.token from   t_txby_productDeviceInit  t, t_txby_deviceXGtoken t2,t_txby_sickDetailInfo t3   WHERE t.deviceSN=t2.deviceSN and t.bedNO=t3.bedNO and t.deptId=t.deptId and t.sickAreaNO=t3.sickAreaNO  and t3.outHosTime is null and   t.deviceSN=t2.deviceSN  and t.deviceUserType<>3  ";
				if(!ChkUtil.isEmpty(jogin)){
					sql+=" and t.deptId in("+jogin+")";
				}else{
					sql+=" and 1=2 ";
				}
				List<Map> l=  this.mBaseDao.findBySql(sql, null, null);
				for(Map data:l){
					if(!ChkUtil.isEmpty(data.get("token"))){
						DeviceRemindJobMsg msg=new DeviceRemindJobMsg();
						try {
							Date date=new Date();
							BeanUtils.copyProperties(msg, drjob);
							msg.setId_(null);
							msg.setTitle(drjob.getParams());
							msg.setCreateTime(date);
							msg.setUpdateTime(date);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						msg.setDeviceSN(data.get("deviceSN").toString());
						int msgId=this.mBaseDao.saveObject(msg);
//						CommonXGRetunInfo result=XGPushUtil.getNewInstance(new Long(dxs.getAccessId()), dxs.getSecretKey()).pushSingleDeviceCustomMessage(data.get("token").toString(), drjob.getParams(), drjob.getContent(), 2, drjob.getRemindType());
						
						//推送消息
		    			Map m=new HashMap();
		    			Map custom=new HashMap();
		    			custom.put("remindType", drjob.getRemindType());
		    			custom.put("discription", customeParam.get(drjob.getRemindType()));
		    			custom.put("msgId", msgId);
//						CommonXGRetunInfo result=xu.pushSingleDeviceCustomMessage(data.get("token").toString(), drjob.getParams(), drjob.getContent(), 2, drjob.getRemindType());
//						CommonXGRetunInfo  result=xgu.pushSingleDeviceCustomMessage(data.get("token").toString(), drjob.getParams(), drjob.getContent(), custom, 2);
						
						CommonXGRetunInfo  result=xu.pushSingleDeviceCustomMessage(data.get("token").toString(), custom);
						System.out.println("推送信息为："+drjob.getContent());
						System.out.println("推送状态为："+result);
					}
					
				}
			}
			
		
		System.out.println("参数为："+params);
		logger.info("RemindTask executeRemind()方法，正在被执行");
	}
	
/*	public String getJoinSql(){
		SysUserEntity user= (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
		String join="";
		//如果不是超级管理员，则只能查询本组织及子组织数据
//		 join=sysDeptService.getSubDeptIdList(user.getDeptId());
		if(user.getUserId() != Constant.SUPER_ADMIN){
			 join=sysDeptService.getSubDeptIdList(user.getDeptId());
		}
		return join;
	}*/
	 
}
